package com.kgis.fm.business.mapadapter.service.impl;

import java.util.List;
import java.util.Map;

import com.kgis.fm.business.constant.FMDBConst;
import com.kgis.fm.business.mapadapter.service.MapAdapterManagementService;
import com.kgis.fm.business.model.GeometryEntity;
import com.kgis.fm.platform.access.metadata.service.ComponentAccessService;
import com.kgis.fm.platform.access.term.service.LegendEntryAccessService;
import com.kgis.fm.platform.access.term.service.PointStyleAccessService;
import com.kgis.fm.platform.access.term.service.StyleRuleAccessService;
import com.kgis.fm.platform.model.constant.ComponentTypeConst;
import com.kgis.fm.platform.model.metadata.Component;
import com.kgis.fm.platform.model.term.LegendEntry;
import com.kgis.fm.platform.model.term.PointStyle;
import com.kgis.fm.platform.model.term.StyleRule;
import com.kgis.map.model.geometry.IGeometry;
import com.kgis.map.service.SpatialDataManagementService;

/**
 * 
 * map适配器实现
 * 
 * @author zhxiaomiao
 * 
 */
public class KGISMapAdapterMangermentServiceImpl implements MapAdapterManagementService {
	private SpatialDataManagementService spatialDataManagementService;
	private ComponentAccessService componentAccessService;
	private LegendEntryAccessService legendEntryAccessService;
	private StyleRuleAccessService styleRuleAccessService;
	private PointStyleAccessService pointStyleAccessService;

	public PointStyleAccessService getPointStyleAccessService() {
		return pointStyleAccessService;
	}

	public void setPointStyleAccessService(PointStyleAccessService pointStyleAccessService) {
		this.pointStyleAccessService = pointStyleAccessService;
	}

	public StyleRuleAccessService getStyleRuleAccessService() {
		return styleRuleAccessService;
	}

	public void setStyleRuleAccessService(StyleRuleAccessService styleRuleAccessService) {
		this.styleRuleAccessService = styleRuleAccessService;
	}

	public ComponentAccessService getComponentAccessService() {
		return componentAccessService;
	}

	public LegendEntryAccessService getLegendEntryAccessService() {
		return legendEntryAccessService;
	}

	public void setLegendEntryAccessService(LegendEntryAccessService legendEntryAccessService) {
		this.legendEntryAccessService = legendEntryAccessService;
	}

	public void setComponentAccessService(ComponentAccessService componentAccessService) {
		this.componentAccessService = componentAccessService;
	}

	public SpatialDataManagementService getSpatialDataManagementService() {
		return spatialDataManagementService;
	}

	public void setSpatialDataManagementService(SpatialDataManagementService spatialDataManagementService) {
		this.spatialDataManagementService = spatialDataManagementService;
	}

	public void createSpatial(GeometryEntity geometryEntity) {
		int layerId = 0;
		Map<String, Object> fieldMap = convertFieldMap(geometryEntity);
		Component component = componentAccessService.findById(geometryEntity.getCno());
		List<LegendEntry> legendEntityList = legendEntryAccessService.findByFnoCno(geometryEntity.getFno(), geometryEntity.getCno());
		if (legendEntityList != null && legendEntityList.size() > 0) {
			layerId = legendEntityList.get(0).getLeno();
		}
		if (geometryEntity.getDetailId() > 0) {
			// 往详图插记录
			if (component.isBdetail()) {
				fieldMap.put(GeometryEntity.Column_DETAILID, geometryEntity.getDetailId());
			}
			// 同一个cno可能对应多个layerid，但是空间表以及索引表都是同一个，因此随便取一个layerid，插入空间表即可。
		}
		if (!geometryEntity.getAttributes().containsKey(GeometryEntity.Column_OBJECTID)) {
			geometryEntity.getAttributes().put(GeometryEntity.Column_OBJECTID, geometryEntity.getFid());
		}
		// 判断是否有角度字段,如果有则需要在fieldmap里面添加角度值
		if (legendEntityList.get(0).getLayerType() == ComponentTypeConst.pointCom) {
			String angleName = getAngleFieldName(legendEntityList);
			if (null != angleName && !("".equals(angleName))) {
				fieldMap.put(angleName.toUpperCase(), geometryEntity.getAngle());
			}
		}
		spatialDataManagementService.insertSpatialData(layerId, geometryEntity.getFid(), (IGeometry) geometryEntity.getGeometry(), fieldMap);
	}

	private Map<String, Object> convertFieldMap(GeometryEntity geometryEntity) {
		Map<String, Object> fieldMap=geometryEntity.getAttributes();
		if(!fieldMap.containsKey(FMDBConst.KGM_FNO)){
			fieldMap.put(FMDBConst.KGM_FNO, geometryEntity.getFno());
		}
		if(!fieldMap.containsKey(FMDBConst.KGM_CNO)){
			fieldMap.put(FMDBConst.KGM_CNO, geometryEntity.getCno());
		}
		if(!fieldMap.containsKey(FMDBConst.KGM_FID)){
			fieldMap.put(FMDBConst.KGM_FID, geometryEntity.getFid());
		}
		return fieldMap;
	}

	private String getAngleFieldName(List<LegendEntry> legendEntityList) {
		int srno = legendEntityList.get(0).getSrno();
		StyleRule styleRule = styleRuleAccessService.findById(srno);
		if(styleRule!=null){
			PointStyle pointStyle = pointStyleAccessService.findById(styleRule.getSno());
			return pointStyle.getAngle();
		}
		return null;
	}

	@Override
	public void removeSpatial(GeometryEntity entity) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeSpatialOnly(GeometryEntity entity) {
		// TODO Auto-generated method stub

	}

	@Override
	public void saveSpatial(GeometryEntity entity) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateLabel(GeometryEntity entity) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateSpatial(GeometryEntity entity) {
		// TODO Auto-generated method stub

	}

}
